<?php
namespace app\course\validate\coupon\admin;

use think\Validate;

/**
 * 优惠券数据验证
 */
class CouponCard extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'mhm_id'   => 'require',
        'issue_no' => 'require',
        'price'    => 'require|gt:0|lt:maxprice',
        'count'    => 'requireIf:online,1|checkCount',

    ];

    protected $field = [
        'mhm_id'   => '机构',
        'issue_no' => '期号',
        'price'    => '优惠金额',
        'count'    => '数量',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'issue_no.require' => '请输入期号',
        'mhm_id.require'   => '请选择机构',
        'price.require'    => '优惠金额必须大于0',
        'price.lt'         => '优惠金额需小于满减金额',
        'count'            => '优惠券数量必须大于0',
    ];

    protected function checkCount($value, $rule, $data)
    {
        if ($data['online'] == 1 && $value < 1) {
            return '优惠券数量必须大于0';
        }

        return true;
    }
}
